#version 460
#define GLSLIFY 1

layout(points) in;
//layout(triangle_strip, max_vertices=128) out;
layout(line_strip, max_vertices=32) out;
//layout(points, max_vertices=64) out;

#define GLSLIFY 1
#define PROJECT_MATRIX

/*
0 投影矩阵
1 属性参数
2 变换数据
3 颜色数据

4 顶点数据
5 顶点配置参数


6 颜色控制参数

10 纹理数据

*/

layout(std140, set=0, binding=0) uniform U_Matrix {
	mat4 mat_ProjecView[2];
};

layout(std430, column_major, binding = 1) readonly buffer U1_Matrix {
	mat4 mat_Model[];
};

//====================== 顶点阶段输入数据 =====================
in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	vec4 vCubeSize;
	uint vInstance;
}vertex_in[];

//======================= 几何阶段输出数据 ========================
layout(location = 0) out vec4 gColor;
/*layout(location = 1) out VS_FS_VERTEX {
	vec4		gColor;
	flat ivec3	vUV_Offset;
	flat ivec2	vUV_Bound;
};*/

void main(void){
	// 位置  大小  对齐方式  深度
	mat4 mat = mat_Model[vertex_in[0].vInstance];
	//摄像机逆矩阵
	//mat4 mr = transpose(mat_ProjecView[1]);

	vec4 pos = gl_in[0].gl_Position;
	vec4 size = vertex_in[0].vCubeSize;
	//size = vec4(3,3,3,1);

	vec4 v = pos;
	v.xyz -= size.xyz * 0.5f;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.x += size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	
	v.z += size.z;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.x -= size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.z -= size.z;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

	v = pos;
	v.xyz += size.xyz * 0.5f;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.z -= size.z;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.x -= size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.z += size.z;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.x += size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

	v = pos;
	v.xyz -= size.xyz * 0.5f;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.y += size.y;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

	v.x += size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();

	v.y -= size.y;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

	v.z += size.z;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	
	v.y += size.y;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

	v.x -= size.x;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	
	v.y -= size.y;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
	EmitVertex();
	EndPrimitive();

}

 